package com.liuxu.autorun;

import android.Manifest;
import android.accessibilityservice.AccessibilityService;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Handler;
import android.support.v4.app.ActivityCompat;
import android.telephony.TelephonyManager;
import android.view.accessibility.AccessibilityEvent;
import android.view.accessibility.AccessibilityNodeInfo;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

public class  onAccessibilityEvent extends AccessibilityService {
    public int search_count=10;
    int time = 50;
    @Override
    public void onAccessibilityEvent(AccessibilityEvent event) {
        String  nowPackageName = event.getPackageName().toString();
        String  nowClassName = event.getClassName().toString();
        utils Helper=new utils();
        if(nowPackageName.equals("com.liuxu.autorun")&&nowClassName.equals("android.widget.Button")){
        int eventType = event.getEventType();
        switch (eventType) {
            case AccessibilityEvent.TYPE_VIEW_CLICKED:
                //界面点击
                AccessibilityNodeInfo nodeInfo = getRootInActiveWindow();
                String vision=getVersion(getApplicationContext());
                String token=nodeInfo.findAccessibilityNodeInfosByViewId("com.liuxu.autorun:id/token").get(0).getText().toString();
                String word=nodeInfo.findAccessibilityNodeInfosByViewId("com.liuxu.autorun:id/word").get(0).getText().toString();
                Iden ider=new Iden();
                boolean allow=ider.run(vision,token,handler);
                while (true) {
                    performGlobalAction(AccessibilityService.GLOBAL_ACTION_HOME);
                    Helper.execShellCmd("am force-stop com.tencent.reading");
                    Helper.execShellCmd("pm clear com.tencent.reading");
                    try {
                        Thread.sleep(1000);
                        Helper.Click(findNodesByText("天天快报"));
                        search_count=1;
                        while (findNodesById("com.tencent.reading++:id/search_btn").size() == 0){
                            Helper.Back(findNodesById("com.tencent.reading:id/permission_guide_close"));
                            Thread.sleep(1000);
                            time-=1;
                            if (time<1){
                                break;
                            }
                        }
                        Thread.sleep(4000);
                        Helper.Back(findNodesById("com.tencent.reading:id/permission_guide_close"));
                        search_count=10;
                        Helper.Click(findNodesById("com.tencent.reading:id/search_btn"));
                        Helper.set_text(findNodesById("com.tencent.reading:id/inputSearch"), word);
                        Helper.Click(findNodesById("com.tencent.reading:id/btnCancel"));
                        Helper.Click(findNodesByText("企鹅号"));
                        Thread.sleep(1000);
//                        Helper.Click(findNodesById("com.tencent.reading:id/rss_name"));
                        Helper.Click(findNodesById("com.tencent.reading:id/rss_arrow_icon_btn"));
                        Thread.sleep(2000);
                        performGlobalAction(AccessibilityService.GLOBAL_ACTION_BACK);
                        Thread.sleep(1000);
                        performGlobalAction(AccessibilityService.GLOBAL_ACTION_BACK);
                        Helper.Click(findNodesByText("未登录"));
                        Helper.Click(findNodesById("com.tencent.reading:id/mine_tab_better_my_focus_item_image"));
                        Thread.sleep(3000);
                        Helper.Click(findNodesById("com.tencent.reading:id/list_title_text"));
                        time = 50;
                        search_count=1;
                        while (time > 0) {
                            if (findNodesById("com.tencent.reading:id/like_icon").size() == 0) {
                                Helper.swipe(300, 750, 300, 250, 300);
                                time -= 1;
                            } else {
                                Helper.Click(findNodesById("com.tencent.reading:id/like_icon"));
                                break;
                            }
                        }
                        search_count=20;
                        Thread.sleep(1000);
                    } catch (Exception e) {
                        continue;
                    }
                    performGlobalAction(AccessibilityService.GLOBAL_ACTION_BACK);
                    performGlobalAction(AccessibilityService.GLOBAL_ACTION_HOME);
                }
//            case AccessibilityEvent.TYPE_VIEW_TEXT_CHANGED:
                //界面文字改动
//                break;
//            case AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED:
        }
        }
    }

    public List<AccessibilityNodeInfo> findNodesById(String viewId) throws InterruptedException {
        List<AccessibilityNodeInfo> result = new ArrayList<>();
        int time=search_count;
        while (result.size() == 0&&time>0){
            AccessibilityNodeInfo nodeInfo = getRootInActiveWindow();
            if (nodeInfo != null) {
                result=nodeInfo.findAccessibilityNodeInfosByViewId(viewId);
            }
            Thread.sleep(1000);
            time-=1;
        }
        return result;
    }

    public List<AccessibilityNodeInfo> findNodesByText(String text) throws InterruptedException {
        List<AccessibilityNodeInfo> result = new ArrayList<>();
        int time=search_count;
        while (result.size() == 0&&time>0){
            AccessibilityNodeInfo nodeInfo = getRootInActiveWindow();
            if (nodeInfo != null) {
                result=nodeInfo.findAccessibilityNodeInfosByText(text);
            }
            Thread.sleep(1000);
            time-=1;
        }
        return result;
    }
    public static String getVersion(Context Context) {
        String verName = "";
        try {
            verName = Context.getPackageManager().
                    getPackageInfo(Context.getPackageName(), 0).versionName;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return verName;
    }
    private Handler handler = new Handler(){
        //写handler的handlermessage方法,用来接收子线程中发来的消息
        public void handleMessage(android.os.Message msg) {
            String text=(String)msg.obj;
            Toast.makeText(getApplicationContext(), text, Toast.LENGTH_SHORT).show();
        };
    };
        public void onInterrupt () {
        }

    }